import sys

import time

from framework.common.pt_logger import logger


class ServerlessSpider(object):
    def __init__(self, request, task_pool, parsers_map, outputs, **kwargs):
        self.request = request
        self.task_pool = task_pool
        self.current_task = None
        self.parsers_map = parsers_map
        self.outputs = outputs
        self.parameters = kwargs
        # self.signal_processors = list()
        # self.queue_empty_retry_times_max = 5
        # self.queue_empty_retry_times = 0
        self.task_generator = None
        # self.unknown_error_max_retry = 10
        # self.unknown_error_current_retry = 0

    def listen_and_start(self, task=None, get_prefix_func=None):
        try:
            if not task:
                task = self.task_pool.dequeue()
            self.current_task = task
            if self.current_task is None:  # 队列为空，则这一次逻辑全局跳过
                return
            response = self._request()
            results, tasks = self._get_parser(self.current_task.parser).parse(response, self.current_task)
            # process results
            if isinstance(self.outputs, list):
                [output.dump(self.current_task, results) for output in self.outputs]
            else:
                self.outputs.dump(self.current_task, results)
            # enqueue tasks if more
            if tasks:
                self.task_pool.enqueue(tasks)
        except Exception as ex:
            # self.unknown_error_current_retry += 1
            logger.error("task processed failed, following is the detail...")
            raise ex
            # if self.unknown_error_current_retry > self.unknown_error_max_retry:
            #     return
            # time.sleep(60)
            # raise ex
            # logger.error(ex)

    def _request(self):
        url = self.current_task.url
        response = self.request.request(url)
        return response


    def _get_parser(self, parser):
        if parser in self.parsers_map:
            return self.parsers_map[parser]
        else:
            return self.parsers_map["default"]
